package jdklearning.net.bio;

import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.*;

/**
 * @author shenenlu 2021年06月11日 下午17:11:05
 */
public class BioClient implements Runnable{


    @Override
    public void run() {
        try {
            ServerSocket serverSocket =
                    new ServerSocket(9999);
            Socket accept = serverSocket.accept();
            byte[] bytes = new byte[1024];
            int read = accept.getInputStream().read(bytes);
            System.out.println(new String(bytes));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException, InterruptedException {
        Socket socket = new Socket();
        InetSocketAddress inetSocketAddress = new InetSocketAddress
                ("localhost",9999);
        socket.connect(inetSocketAddress);
//        socket.setKeepAlive(true);
        OutputStream outputStream = socket.getOutputStream();
        InputStream inputStream = socket.getInputStream();


        JFrame jFrame = new JFrame();
        jFrame.setTitle("test");
        jFrame.setSize(500,500);
        jFrame.setLocation(400,200);//位置
        jFrame.setSize(350,200);//尺寸
        jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //创建流式布局对象(左对齐,水平间距20,垂直间距40)
        FlowLayout fl=new FlowLayout(FlowLayout.LEFT,20,40);
        jFrame.setLayout(fl);

        JButton jb = new JButton("test");
        jFrame.add(jb);

        JTextField jTextField = new JTextField("",10);
        jFrame.add(jTextField);
        jFrame.setVisible(true);

        byte[] bytes = new byte[1024];
        while ( inputStream.read(bytes) != -1 ){
            jTextField.setText( new String(bytes));
        }

    }
}
